﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Configuration;

using System.Configuration;
using System.Net;
using System.Collections.Specialized;
using System.Diagnostics;


using MySql.Data;
using MySql.Data.MySqlClient;

namespace doris.Ticketuser
{
    public partial class EditUser : System.Web.UI.Page
    {
        
        MySqlConnection con;
        MySqlCommand cmd;
        String username = "";
        String fullname = "";
        String adress = "";
        String postcode = "";
        String email    = "";
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                if (null != HttpContext.Current && null != HttpContext.Current.Session &&
                    null != HttpContext.Current.Session["username"])
                {
                    username = Session["username"].ToString();
                    fullname = Session["fullname"].ToString();
                    adress = Session["adress"].ToString();
                    postcode = Session["postcode"].ToString();
                    email = Session["email"].ToString();
                    

                    Label1.Text = username;
                    NameBox.Text = fullname;
                    EmailBox.Text = email;
                    PostcodeBox.Text = postcode;
                    AddressBox.Text = adress;
                }
                else
                {
                    Label1.Text = "You are not logged on, please try again ;-(";
                }
            }
    
        }

        void getAddresse()
        {
            Label4.Text = "";
            this.setConnection();
            MySqlDataReader reader;
            String adr ="";
            postcode = PostcodeBox.Text;
            try
            {
                con.Open();
                cmd = new MySqlCommand("SELECT txt FROM postcode WHERE postcode = @postcode", con);
                cmd.Parameters.AddWithValue("@postcode",postcode);
                reader = cmd.ExecuteReader();

                while(reader.Read())
                {
                    adr = reader["txt"].ToString();
                }



                if (adr == "")
                {
                    PostcodeBox.Text = Session["postcode"].ToString();
                    AddressBox.Text = Session["adress"].ToString();
                    Label4.Text = "This Postcode doesn't exist!";
                }
                else
                {
                    AddressBox.Text = adr;
                    Session["adress"] = adr;
                    Session["postcode"] = postcode;
                }
                
            }
            
            catch (MySqlException ex)
            {
                Label1.Text = ex.ToString();
            }
            finally
            {
                con.Close();
            }
            
        }

       

        protected void CancelButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("Usermain.aspx");
        }

        public void setConnection()
        {
            con = new MySqlConnection(WebConfigurationManager.ConnectionStrings["localConnectionString"].ConnectionString);
        }

        protected void OkButton_Click(object sender, EventArgs e)
        {
            //If no error
           if (Label5.Text == "" && Label6.Text == "")
             {
                Response.Redirect("comfirmEditUser.aspx");
             }
            
        }
        protected void PostcodeBox_TextChanged(object sender, EventArgs e)
        {
            this.setConnection();
            this.getAddresse();
        }

        protected void NameBox_TextChanged(object sender, EventArgs e)
        {
            Label5.Text = "";
            //Check if postcodeBox is changed. 
            //If Changed = rest ist unchaged
            
                if (NameBox.Text == "")
                {

                    Label5.Text = "Cannot be emty!";
                }
                else
                {
                    Session["fullname"] = NameBox.Text;
                }         
        }
       
        protected void EmailBox_TextChanged(object sender, EventArgs e)
        {
            Label5.Text = "";
            //Check if postcodeBox is changed. 
            //If Changed = rest ist unchaged
            
                if (EmailBox.Text == "")
                {

                    Label6.Text = "Cannot be emty!";
                }
                else
                {
                    Session["email"] = EmailBox.Text;
                }
        }
   

        protected void AddressBox_TextChanged(object sender, EventArgs e)
        {
              

        }
       

        protected void Button1_Click(object sender, EventArgs e)
        {
          
        }

      
     
    }


}